package com.example.yichuangyun.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.DialogVersionUpdateBinding;
import com.example.yichuangyun.utils.CommonUtil;
import com.example.yichuangyun.utils.DensityUtil;

/**
 * 版本更新
 */
public abstract class VersionUpdateDialog {
    private Dialog dialog;
    private DialogVersionUpdateBinding binding;

    public VersionUpdateDialog(Activity activity) {
        if (dialog == null) {
            getDialog(activity);
        }
    }

    private void getDialog(Activity activity) {
        binding = DialogVersionUpdateBinding.inflate(LayoutInflater.from(activity));
        dialog = new Dialog(activity, R.style.prompt_progress_dialog_dimenabled);
        dialog.setContentView(binding.getRoot(), new LinearLayout.LayoutParams
                (LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
        dialog.setCancelable(false);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        dialogWindow.setGravity(Gravity.TOP);
        lp.y = DensityUtil.dip2px(activity, 150); // 新位置Y坐标
        dialogWindow.setAttributes(lp);
        binding.tvUpdate.setOnClickListener(v -> {
            if (CommonUtil.isCanClick()) {
                confirm();
            }
        });
    }

    public abstract void confirm();

    public void setVersion(String version) {
        binding.tvVersionCode.setText("V" + version);
    }

    public void showDialog() {
        dialog.show();
    }

    public void dismissDialog() {
        if (dialog != null) {
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }
    }
}
